package com.geeyuu.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class BasePrefrence {

    private static SharedPreferences sp = null;

    private synchronized static void init(Context context) {
        if (null == sp) {
            sp = PreferenceManager.getDefaultSharedPreferences(context);
        }
    }

    protected synchronized static void clearAll(Context context) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.clear();
            editor.commit();
        }
    }

    protected synchronized static void removeKey(Context context, String key) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.remove(key);
            editor.commit();
        }
    }

    protected synchronized static void putBoolean(Context context, String key, boolean param) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.putBoolean(key, param);
            editor.commit();
        }
    }

    protected synchronized static boolean getBoolean(Context context, String key, boolean defaultValue) {
        init(context);
        synchronized (sp) {
            return sp.getBoolean(key, defaultValue);
        }
    }

    protected synchronized static void putString(Context context, String key, String param) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.putString(key, param);
            editor.commit();
        }
    }

    protected synchronized static String getString(Context context, String key) {
        init(context);
        synchronized (sp) {
            return sp.getString(key, "");
        }
    }

    protected synchronized static void putInt(Context context, String key, int param) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.putInt(key, param);
            editor.commit();
        }
    }

    protected synchronized static int getInt(Context context, String key) {
        init(context);
        synchronized (sp) {
            return sp.getInt(key, 0);
        }
    }

    protected synchronized static void putLong(Context context, String key, long param) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.putLong(key, param);
            editor.commit();
        }
    }

    protected synchronized static long getLong(Context context, String key) {
        init(context);
        synchronized (sp) {
            return sp.getLong(key, 0l);
        }
    }

    protected synchronized static void putFloat(Context context, String key, float param) {
        init(context);
        synchronized (sp) {
            Editor editor = sp.edit();
            editor.putFloat(key, param);
            editor.commit();
        }
    }

    protected synchronized static float getFloat(Context context, String key) {
        init(context);
        synchronized (sp) {
            return sp.getFloat(key, 0f);
        }
    }

}
